<?php
namespace app\common\model;

use app\common\model\TagModel;
use think\Model;

class CustomerModel extends BaseModel{

    protected $name = 'boyi_customer';
    protected $pk = 'id';
    public $append = [
        'died_line_info',
        'died_line_time_str'
    ];

    
    public function tagList(){
        return $this->belongsToMany(TagModel::class, TagRelModel::class,  'tag_id', 'obj_id');
    }
    
    public function jobList(){
        return $this->hasMany(JobModel::class, 'customer_id', 'id')->alias('job')->join("boyi_job_type jt", "jt.id = job_type_id")->field('job.*, jt.name,jt.short_name')->with(['job_add_user', 'job_exec_user', 'file_list']);
    }

    
    public function school(){
        return $this->hasOne(SchoolModel::class, 'id', 'schoolCode')->cache(true, 3600);
    }

    
    public function institution(){
        return $this->hasOne(InstitutionModel::class, 'id', 'institutionId')->cache(true, 3600);
    }

    public function getDiedLineTimeStrAttr(){
        return $this->diedline ? date('Y-m-d', $this->diedline) : '';
    }   

    public function getDiedLineInfoAttr(){

        if($this->diedline == 0){
            return [
                'str' => '未设置截止日期',
                'tag_type' => 'warning',
            ];
        }


        $diff = $this->diedline - time();
        $day = $diff / (3600 * 24);

        // 3 天为危险颜色， 7天为警告颜色
        // 逾期为危险颜色
        
        if(0 < $day && $day < 1){
            // 转化为小时
            $hour = $diff / 3600;

            $result = '剩余' . abs(floor($hour)) . '小时';
            $tag_type = 'warning';
        }else{
            $tag_type = 'warning';
            $result = '剩余' . floor($day) . '天';
        }
        if($day < 0){
            $result = '已截止';
            $tag_type = 'warning';
        }elseif($day <= 3){
            $tag_type = 'danger';
        }elseif($day <= 7){
            $tag_type = 'danger';
        }else{  
            $tag_type = 'success';
        }
        return [
            'str' => $result,
            'tag_type' => $tag_type,
        ];
    }

    // 通过 CLUE PROGRESS 模型获取选题进度列表
    public function getClueProgressListAttr(){
        // dump();
        // dump($this->school['schoolName']);
        // dump($this->school['clueProgressList']);
        $progressTypeNamelist = explode(',', $this->school['clueProgressList']);
        $result = [];

        $current_progress_list = $this->clue_progress ? $this->clue_progress->toArray() : [];
        $current_progress_list = array_combine(array_column($current_progress_list, 'classNameType'), $current_progress_list);
        foreach ($progressTypeNamelist as $classNameType) {
            if(isset(JobProgressModel::CLUE_PROGRESS_INFO[$classNameType])) {
                $tmp_arr = JobProgressModel::CLUE_PROGRESS_INFO[$classNameType];
                if(isset($current_progress_list[$classNameType])){
                    $tmp_arr['status'] = $current_progress_list[$classNameType]['status'];
                }else{
                    $tmp_arr['status'] = JobProgressModel::CLUE_STATUS_UNHANDLE; // 默认为无需处理状态
                }
                $tmp_arr['label'] .= ':' . JobProgressModel::CLUE_STATUS_ARR[$tmp_arr['status']]['info'];

                if(isset($current_progress_list[$classNameType]) && $current_progress_list[$classNameType]['notice_info']){
                    $tmp_arr['label'] .= ' - ' . $current_progress_list[$classNameType]['notice_info'];  
                }

                $tmp_arr['button_type'] = JobProgressModel::CLUE_STATUS_ARR[$tmp_arr['status']]['button_type'];
                $tmp_arr['classNameType'] = $classNameType;
                $result[] = $tmp_arr;
            }
        }
        return $result;
    }


}